package UserDefinedSink;

import bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import org.apache.flink.connector.jdbc.JdbcSink;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.RichSinkFunction;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/**
 * @author Spring_Hu
 * @date 2021/10/12 15:50
 */
public class UserDefinedSink {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env
                .socketTextStream("hadoop102", 8888).map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        })
        /*.addSink(JdbcSink.sink(
                "insert into WaterSensor (id, ts, vc) values (?,?,?) ON DUPLICATE KEY UPDATE ts=VALUES(ts),vc=VALUES(vc)",
                (ps, t) -> {
                    ps.setString(1,t.getId());
                    ps.setLong(2, t.getTs());
                    ps.setInt(3, t.getVc());
                    ps.execute();
                    String tableName = ps.getMetaData().getTableName(0);
                    System.out.println(tableName);
                },
                new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
                        .withUrl("jdbc:mysql://hadoop102:3306/test?useSSL=false")
                        .withDriverName("com.mysql.jdbc.Driver")
                        .withUsername("root")
                        .withPassword("123321")
                        .withConnectionCheckTimeoutSeconds(1)
                        .build()));*/
        .addSink(new RichSinkFunction<WaterSensor>() {
            Connection connection=null;
            PreparedStatement ps=null;
            @Override
            public void close() throws Exception {
                if(ps!=null){
                    ps.close();
                }
                if(connection!=null){
                    connection.close();
                }
            }

            @Override
            public void open(Configuration parameters) throws Exception {
                connection = DriverManager.getConnection("jdbc:mysql://hadoop102:3306/test?useSSL=false","root","123321");
                ps = connection.prepareStatement("insert into WaterSensor (id, ts, vc) values (?,?,?) ON DUPLICATE KEY UPDATE ts=VALUES(ts),vc=VALUES(vc)");

            }

            @Override
            public void invoke(WaterSensor value, Context context) throws Exception {
                System.out.println(context.currentWatermark());
                ps.setString(1,value.getId());
                ps.setLong(2, value.getTs());
                ps.setInt(3, value.getVc());
                ps.execute();
                ps.execute();
            }
        });

        env.execute();



    }
}
